﻿// create angular app
var validationApp = angular.module('validationApp', ['validationApp.directives']);

/* Directives */
angular.module('validationApp.directives', [])
    .directive('pwCheck', [function () {
        return {
            require: 'ngModel',
            link: function (scope, elem, attrs, ctrl) {
                var firstPassword = '#' + attrs.pwCheck;
                elem.add(firstPassword).on('keyup', function () {
                    scope.$apply(function () {
                        // console.info(elem.val() === $(firstPassword).val());
                        ctrl.$setValidity('pwmatch', elem.val() === $(firstPassword).val());
                    });
                });
            }
        }
    }]);

// create angular controller
validationApp.controller('mainController', function ($scope, $location, EventsService) {

    // function to submit the form after all validation has occurred			
    $scope.submitForm = function (user) {
        EventsService.addUser(user).then(function () {
            alert('Registration was successful. You may proceed login.');
            window.location.assign("/Login");
        }, function ()
        { alert('Email address has already been taken.') });
    };
});
